const webpack = require('webpack')
const path = require('path')
const title = require('./package.json').title

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: process.env.NODE_ENV === 'production' ? './' : '/', // 部署应用包时的基本 URL
  // webpack配置
  configureWebpack: {
    name: title,
    plugins: [
      new webpack.DefinePlugin({
        siteConfig: JSON.stringify({
          title: title
        }),
        BUILD_ENV: JSON.stringify(process.env.BUILD_ENV)
      })
    ]
  },

  chainWebpack: config => {
    // set svg-sprite-loader
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  },

  // 生产环境的 source map
  productionSourceMap: false,

  css: {
    // sourceMap: true // 开启 CSS source maps
    sourceMap: process.env.NODE_ENV !== 'production' // 开启 CSS source maps
  }
}
